<!DOCTYPE html>
<html>
  <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta content="yes" name="apple-mobile-web-app-capable" />
  <meta content="black" name="apple-mobile-web-app-status-bar-style" />
  <meta name="referrer" content="never">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <meta name="author" content="kveln">
  <title>Markdown写论文，Pandoc编译，生成Docx | thiswind</title>
  <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
  <!-- <link href="https://www.thiswind.work/media/css/bootstrap.min.css" rel="stylesheet"> -->
  <!--  <link href="https://www.thiswind.work/media/css/all.min.css" rel="stylesheet" type="text/css"> -->
  <link href="https://cdn.bootcss.com/font-awesome/5.11.2/css/all.min.css" rel="stylesheet">
  <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
  <link rel="alternate" type="application/rss+xml" title="Markdown写论文，Pandoc编译，生成Docx | thiswind » Feed" href="https://www.thiswind.work/atom.xml">
  <link rel="stylesheet"href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.10/build/styles/androidstudio.min.css">
  <link href="https://www.thiswind.work/styles/main.css" rel="stylesheet">
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.10/build/highlight.min.js"></script>
  <!-- <script src="https://www.thiswind.work/media/scripts/jquery.min.js"></script> -->
  <script>hljs.initHighlightingOnLoad();</script>
  
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-152960855-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
    function gtag() {
      dataLayer.push(arguments)
    }
    gtag('js', new Date());
    gtag('config', 'UA-152960855-1');</script>
  

    <meta property="og:description" content="Markdown写论文，Pandoc编译，生成Docx"/>
    <meta property="og:url" content="https://www.thiswind.work/post/markdown-xie-lun-wen-pandoc-bian-yi-sheng-cheng-docx/"/>
    <meta property="og:locale" content="zh-CN"/>
    <meta property="og:type" content="website"/>
    <meta property="og:site_name" content="thiswind"/>
  </head>
  <body>
  	<!-- Navigation -->
  <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
    <div class="container">
      <a class="navbar-brand" href="https://www.thiswind.work">thiswind</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        Menu
        <i class="fas fa-bars"></i>
      </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
          
          <li class="nav-item">
              
              <a class="nav-link" href="/">首页</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/archives">归档</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/tags">标签</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/post/about">关于</a>
              
          </li>
          
        </ul>
      </div>
    </div>
  </nav>
  <!-- Page Header -->
  <header class="masthead" style="background-image: url('https://www.thiswind.work/media/images/home-bg.jpg')">
    <div class="overlay"></div>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <div class="post-heading">
          	<span class="tags">
          	 
        </span>
            <h1>Markdown写论文，Pandoc编译，生成Docx</h1>
            <span class="meta">
            	Posted on
              2019-11-20，4 min read
            </span>
          </div>
        </div>
      </div>
    </div>
  </header>

  <!-- Post Content -->
  <article>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <h2 id="目录结构">目录结构</h2>
<pre><code class="language-bash">[ONE]&gt; tree -d
.
├── release
└── src
</code></pre>
<p>源代码放在 <code>src</code> 目录当中<br>
编译结果放在 <code>release</code> 目录当中</p>
<h2 id="前提条件">前提条件</h2>
<ul>
<li><code>src/chinese-gb7714-2005-numeric.csl</code> : <a href="/post/zotero-style-repository/">下载</a></li>
<li><code>src/my-reference.docx</code>   : 自己创建一个</li>
</ul>
<p>模板的创建方法：</p>
<ol>
<li>用一个空的 docx 文件作为模板，编译文本</li>
<li>在编译出来的文本当中，根据需要修改各种样式的字体、字号、段落等</li>
<li>将修改好的docx文件重命名为<code>my-reference.docx</code>，用作模板</li>
</ol>
<!-- more -->
<h2 id="bib格式的参考文献在markdown当中的引用方式"><code>.bib</code>格式的参考文献在markdown当中的引用方式</h2>
<p><code>.bib</code> 文件必须和 <code>.md</code> 文件同名</p>
<p>论文.bib</p>
<pre><code class="language-bib">@article{吴爱华2019以,
title={以 “新工科” 建设引领高等教育创新变革},
author={吴爱华 and 杨秋波 and 郝杰},
journal={高等工程教育研究},
number={1},
pages={1--7},
year={2019}
}
</code></pre>
<p>论文.md</p>
<pre><code class="language-markdown">这是引用了吴爱华关于新工科的论述[吴爱华2019以]
</code></pre>
<h2 id="编译脚本">编译脚本</h2>
<p>build.sh</p>
<pre><code class="language-bash">#!/bin/bash

for dep in &quot;pandoc&quot; &quot;pandoc-citeproc&quot; &quot;tree&quot;; do
  printf &quot;check %s...&quot; &quot;$dep&quot;
  if test -z &quot;$(command -v $dep)&quot;; then
    printf &quot;Please Install %s&quot; &quot;$dep&quot;
    exit
  else
    printf &quot;OK.&quot;
  fi
  printf &quot;\n&quot;
done

# store base path
base=$(pwd)

# define paths
src=&quot;src&quot;
build=&quot;build&quot;
release=&quot;release&quot;

# prepare dirs
for dir in $build $release; do
  rm -rf $dir
  mkdir -p $dir
done

# copy sources to build
cp -r $src/* $build/
cp -r $src/.* $build/

# cd into build to do build processes
cd $build || exit

# build each .md file with pandoc
# NOTICE: each .md file must compair with a .bib file with the same name
# - hello.md
# - hello.bib
#
for name in $(/bin/ls | grep &quot;.md&quot;); do
  name=$(echo &quot;$name&quot; | cut -d &quot;.&quot; -f 1)

  if test ! -f &quot;$name&quot;.bib; then
    touch &quot;$name&quot;.bib
  fi

  printf &quot;[%s] building&quot; &quot;$name&quot;

  printf &quot; ... &quot;

  #    --pdf-engine=xelatex \
  pandoc \
    --filter pandoc-citeproc \
    --bibliography=&quot;$name&quot;.bib \
    --csl=chinese-gb7714-2005-numeric.csl \
    -f markdown \
    -t docx \
    --reference-doc=my-reference.docx \
    -o &quot;$name&quot;.docx \
    &quot;$name&quot;.md

  if test -f &quot;$name&quot;.docx; then
    printf &quot; OK. &quot;

    mv &quot;$name&quot;.docx &quot;$base&quot;/&quot;$release&quot;/
    printf &quot;(moved to release)\n&quot;
  fi
done

cd &quot;$base&quot; || exit

tree -N -h $release

</code></pre>
<h2 id="gitlab-持续集成脚本">Gitlab 持续集成脚本</h2>
<p>.gitlab-ci.yml</p>
<pre><code class="language-yaml">build:
  image: ntwrkguru/pandoc-gitlab-ci
  script:
    - apt-get update &amp;&amp; apt-get install -y tree
    - /bin/bash build.sh

  # 为了本地运行，cache的路径必须和artifacts的路径一致
  cache: &amp;build_artifact_cache
    key: build
    paths:
      - release/

  artifacts:
    paths:
      - release/
</code></pre>
<h2 id="本地运行-gitlab-runner-的命令">本地运行 gitlab-runner 的命令</h2>
<p>command line</p>
<pre><code class="language-bash">gitlab-runner exec docker --docker-volumes $(pwd)/cache:/cache build
</code></pre>
<p>输出结果会被打包放在: <code>$(pwd)/cache/.../cache.zip</code> 当中</p>
<h2 id="参考文献">参考文献：</h2>
<ul>
<li>https://zhuanlan.zhihu.com/p/34450062</li>
</ul>

          
          <p class="next-post">下一篇：
            <a href="https://www.thiswind.work/post/wo-kan-guo-huo-zhe-xiang-kan-que-mei-you-mai-dao-de-shu/">
              <span class="post-title">
                珊瑚岛上的死光&rarr;
              </span>
            </a>
          </p>
        
        <div class="comment">
          
          
            <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
  var gitalk = new Gitalk({
    clientID: 'd48e8b01472c55f16765',
    clientSecret: '782222cbe7945d7e46322b65bc58e589e5887268',
    repo: 'thiswind.github.io',
    owner: 'thiswind',
    admin: ['thiswind'],
    id: (location.pathname).substring(0, 49),      // Ensure uniqueness and length less than 50
    distractionFreeMode: false  // Facebook-like distraction free mode
  })
  gitalk.render('gitalk-container')
</script>

          
          
        
        </div>
      </div>
    </div>
  </article>
 <!-- Footer -->
  <footer>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <ul class="list-inline text-center">
            
            
              
            
              
            
              
            
              
            
              
            
              
            
              
              <li class="list-inline-item">
              <a href="https://www.thiswind.work/atom.xml" target="_blank">
                <span class="fa-stack fa-lg">
                  <i class="fas fa-circle fa-stack-2x"></i>
                  <i class="fas fa-rss fa-stack-1x fa-inverse"></i>
                </span>
              </a>
              </li>
          </ul>
          <p class="copyright text-muted">Copyright &copy;<span>thiswind</span><br><a href="https://github.com/getgridea/gridea" class="Themeinfo">Powered by Gridea</a></p>
        </div>
      </div>
    </div>
   </footer>
  <!-- Bootstrap core JavaScript -->
  <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
  <!-- <script src="https://www.thiswind.work/media/scripts/bootstrap.bundle.min.js"></script> -->
  <!-- Bootstrap core JavaScript -->
  <script src="https://cdn.jsdelivr.net/gh/Alanrk/clean-cdn@1.0/scripts/clean-blog.min.js"></script>
  <!-- <script src="https://www.thiswind.work/media/scripts/clean-blog.min.js"></script> -->
  <script src="//instant.page/3.0.0" type="module" defer integrity="sha384-OeDn4XE77tdHo8pGtE1apMPmAipjoxUQ++eeJa6EtJCfHlvijigWiJpD7VDPWXV1"></script>
  <style type="text/css">a.back_to_top{text-decoration:none;position:fixed;bottom:40px;right:30px;background:#f0f0f0;height:40px;width:40px;border-radius:50%;line-height:36px;font-size:18px;text-align:center;transition-duration:.5s;transition-propety:background-color;display:none}a.back_to_top span{color:#888}a.back_to_top:hover{cursor:pointer;background:#dfdfdf}a.back_to_top:hover span{color:#555}@media print,screen and(max-width:580px){.back_to_top{display:none!important}}</style>
<a id="back_to_top" href="#" class="back_to_top">
  <span>▲</span></a>
<script>$(document).ready((function(_this) {
    return function() {
      var bt;
      bt = $('#back_to_top');
      if ($(document).width() > 480) {
        $(window).scroll(function() {
          var st;
          st = $(window).scrollTop();
          if (st > 30) {
            return bt.css('display', 'block')
          } else {
            return bt.css('display', 'none')
          }
        });
        return bt.click(function() {
          $('body,html').animate({
            scrollTop: 0
          },
          800);
          return false
        })
      }
    }
  })(this));</script>
  </body>
</html>

